package cn.zhfeat.hatch.web.service.impl.system;

import java.util.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.zhfeat.core.base.BaseResultModel;
import cn.zhfeat.core.base.ResultStatus;
import cn.zhfeat.core.shiro.ShiroSessionData;
import cn.zhfeat.core.shiro.bean.ShiroSysAuthorityBean;
import cn.zhfeat.hatch.dto.company.CompanyDepartmentDto;
import cn.zhfeat.hatch.dto.store.BaseStoreInfoDto;
import cn.zhfeat.hatch.dto.store.BaseStoreToUserDto;
import cn.zhfeat.hatch.dto.sysuser.*;
import cn.zhfeat.hatch.entity.company.CompanyDepartment;
import cn.zhfeat.hatch.entity.company.CompanyInfo;
import cn.zhfeat.hatch.entity.store.BaseStoreInfo;
import cn.zhfeat.hatch.entity.store.BaseStoreToUser;
import cn.zhfeat.hatch.entity.sysuser.*;
import cn.zhfeat.hatch.rpc.*;
import cn.zhfeat.hatch.web.service.IAccountService;
import cn.zhfeat.hatch.web.service.ILoginService;
import cn.zhfeat.hatch.web.utils.ConfigUtil;
import cn.zhfeat.hatch.web.utils.ShiroSessionDataUtil;
import cn.zhfeat.utils.CookieUtil;
import cn.zhfeat.utils.MD5Util;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;



/**
 * @description: 账号管理接口实现层,业务服务消费层
 * 	原名：AccountServiceImpl
 *
 * @author ZhangHao
 * @date 2018年5月16日 上午10:47:10
 * @version 1.0.0.1
 */
@Service("accountService")
public class AccountServiceImpl implements IAccountService {


	@Autowired
	private IAccountRpcService accountRpcService;

	@Autowired
	private IBaseStoreInfoRpcService baseStoreInfoRpcService;

	@Autowired
	private IDepartmentRpcService departmentRpcService;

	@Autowired
	private IRoleRpcService roleRpcService;

	@Autowired
	private ILoginService loginService;

	@Autowired
	private ICompanyInfoRpcService companyInfoRpcService;

	/**
	 * @description: 新建名片-弹框列表|分页
	 *		原方法名：getUserAllByNameOrPhone
	 *
	 * @param dto
	 *
	 * @author ZhangHao
	 * @date 2018年5月16日 上午10:03:55
	 * @version 1.0.0.1
	 */
	public BaseResultModel ajaxAccountDialogUserList(SysUserDto dto) {
		BaseResultModel baseResultModel = new BaseResultModel();
		ShiroSessionData shiroSessionData = ShiroSessionDataUtil.getShiroSessionData();
		dto.setShiroSessionData(shiroSessionData);
		dto.setCid(shiroSessionData.getCid());
//		dto.setCid(1L);

		if(dto.getCid() == null) {
			baseResultModel.setStatus(ResultStatus.CID_ERROR.getStatusCode());
			baseResultModel.setMessage(ResultStatus.CID_ERROR.getStatusMessage());
			return baseResultModel;
		}

		PageInfo<SysUser> dialogPageList = accountRpcService.ajaxPageUserListByType(dto);
		if(dialogPageList == null || dialogPageList.getList().size() == 0) {
			baseResultModel.setStatus(ResultStatus.PAGE_LIST_NULL.getStatusCode());
			baseResultModel.setMessage(ResultStatus.PAGE_LIST_NULL.getStatusMessage());
			return baseResultModel;
		}
		for(int i = 0 ; i < dialogPageList.getList().size(); i ++) {
			BaseStoreToUserDto bstu = new BaseStoreToUserDto();
			bstu.setCid(dto.getCid());
			bstu.setUserId(dialogPageList.getList().get(i).getId());
			List<BaseStoreToUser> bstuList = accountRpcService.selectUserStores(bstu);
			BaseStoreInfoDto bsid = new BaseStoreInfoDto();
			bsid.setCid(dto.getCid());
			List<BaseStoreInfo> bsiList = baseStoreInfoRpcService.findListByDto(bsid);

			if(bstuList == null  || bstuList.size() == 0|| bsiList == null || bsiList.size() == 0) {
				continue;
			}
			String str = "";
			for(BaseStoreToUser bs: bstuList) {
				for(BaseStoreInfo bi : bsiList) {
					if(bs.getStoreId().longValue() == bi.getId().longValue()) {
						str += bi.getName() + ",";
					}
				}
			}

			if(StringUtils.isNotBlank(str)) {
				dialogPageList.getList().get(i).setStores(str.substring(0 , str.length()-1));
			}
		}

		baseResultModel.setStatus(ResultStatus.SUCCESS.getStatusCode());
		baseResultModel.setMessage(ResultStatus.SUCCESS.getStatusMessage());
		baseResultModel.setData(dialogPageList.getList());
		baseResultModel.setTotalCount(Integer.parseInt(String.valueOf(dialogPageList.getTotal())));
		return baseResultModel;
	}

	/**
	 * @description: 根据user id 定位一条记录
	 *
	 * @param dto
	 * @author ZhangHao
	 * @date 2018年5月17日 下午8:06:51
	 * @version 1.0.0.1
	 */
	public Object ajaxFindUserInfoById(SysUserDto dto) {
		BaseResultModel baseResultModel = new BaseResultModel();
		if(dto.getId() == null) {
			baseResultModel.setStatus(ResultStatus.CID_ERROR.getStatusCode());
			baseResultModel.setMessage(ResultStatus.CID_ERROR.getStatusMessage());
			return baseResultModel;
		}
		SysUser user = accountRpcService.findUserInfoByType(dto);
		if(user == null) {
			baseResultModel.setStatus(ResultStatus.NO_RECORD.getStatusCode());
			baseResultModel.setMessage(ResultStatus.NO_RECORD.getStatusMessage());
			return baseResultModel;
		}


		baseResultModel.setStatus(ResultStatus.SUCCESS.getStatusCode());
		baseResultModel.setMessage(ResultStatus.SUCCESS.getStatusMessage());
		baseResultModel.setData(user);
		return baseResultModel;
	}
	/**
	 * @description: 根据门店id获取门店下所有用户的信息
	 *				原方法名：getUsersByStoreId
	 * @param dto
	 * @return
	 * @author ZhangHao
	 * @date 2018年5月19日 下午4:13:42
	 * @version 1.0.0.1
	 */
	@Override
	public BaseResultModel findUsersByStoreId(BaseStoreToUserDto dto) {
		ShiroSessionData shiroSessionData = ShiroSessionDataUtil.getShiroSessionData();
		dto.setShiroSessionData(shiroSessionData);
		if(dto.getCid()==null){
			dto.setCid(shiroSessionData.getCid());
		}
		BaseResultModel baseResultModel = new BaseResultModel();
		List<SysUser> userList = new ArrayList<>();
		//根据门店id获取所有用户id
		List<BaseStoreToUser> userStoreList = accountRpcService.selectUserStores(dto);
		//根据用户id查询用户信息
		for (BaseStoreToUser baseStoreToUser : userStoreList) {
			SysUserDto userDto = new SysUserDto();
			userDto.setCid(baseStoreToUser.getCid());
			userDto.setId(baseStoreToUser.getUserId());
			userDto.setStatus(0);
			userDto.setName(dto.getName());
			SysUser user = accountRpcService.findUserInfoByType(userDto);
			if(user!=null){
				userList.add(user);
			}
		}
		if(userList!=null&&userList.size()>0){
			baseResultModel.setData(userList);
			baseResultModel.setStatus(ResultStatus.SUCCESS.getStatusCode());
			baseResultModel.setMessage(ResultStatus.SUCCESS.getStatusMessage());
		}else{
			baseResultModel.setStatus(ResultStatus.NO_RECORD.getStatusCode());
			baseResultModel.setMessage(ResultStatus.NO_RECORD.getStatusMessage());
		}
		return baseResultModel;
	}

	/**
	 * @description: 根据条件查询用户信息
	 *
	 * @param dto
	 * @return
	 * @author ZhangHao
	 * @date 2018年5月20日 下午3:26:07
	 * @version 1.0.0.1
	 */
	@Override
	public SysUser findUserInfoBydto(SysUserDto dto) {
		return accountRpcService.findUserInfoByType(dto);
	}

	/**
	 *
	 * @Description: 更改用户协议状态
	 *
	 * @author: ZhangHao
	 * @date:   2018年5月24日 下午3:07:58
	 * @version 1.0.0.1
	 */
	@Override
	public BaseResultModel ajaxUpdateAgreement() {

		BaseResultModel model = new BaseResultModel();

		ShiroSessionData shiroSessionData = ShiroSessionDataUtil.getShiroSessionData();
		Long userId = shiroSessionData.getUserId();
		if(userId == null) {
			model.setStatus(ResultStatus.NO_RECORD.getStatusCode());
			model.setMessage(ResultStatus.NO_RECORD.getStatusMessage());
			return model;
		}
		//查询用户信息
		SysUserDto userDto = new SysUserDto();
		userDto.setId(userId);
		SysUser sysUser = accountRpcService.findUserInfoByType(userDto);
		if(sysUser == null) {
			model.setStatus(ResultStatus.NO_RECORD.getStatusCode());
			model.setMessage(ResultStatus.NO_RECORD.getStatusMessage());
			return model;
		}
		//更新协议状态
		sysUser.setAgreement(1);
		sysUser.setUpdateTime(new Date());
		Integer result = accountRpcService.updateSelective(sysUser);
		if(result != 1) {
			model.setStatus(ResultStatus.UPDATE_ERROR.getStatusCode());
			model.setMessage(ResultStatus.UPDATE_ERROR.getStatusMessage());
			return model;
		}
		//用户如果同意，更新缓存中的协议状态
		Subject subject = SecurityUtils.getSubject();
		Session session = subject.getSession();
		shiroSessionData.setAgreement(1);
		/** 将用户绑定信息对象转换成JSON*/
		String shiroSessionDataStr = JSON.toJSONString(shiroSessionData);
		/** 把用户绑定信息放到全局登录会话中进行维护*/
		session.setAttribute("sessionUserData", shiroSessionDataStr);
		model.setStatus(ResultStatus.SUCCESS.getStatusCode());
		model.setMessage(ResultStatus.SUCCESS.getStatusMessage());
		return model;
	}

	/**
	 * @description: 根据部门id，查询该部门对应的全部用户信息
	 * 需要返回的数据有：
	 * (用户角色按逗号分隔，用户手机号，用户姓名，用户业务身份按逗号分隔)
	 *
	 * @return
	 * @author ZhangHao
	 * @date 2018年5月24日 下午2:21:12
	 * @version 1.0.0.1
	 */
	@Override
	public Object ajaxSelectUserByDepartment(SysUserDto dto) {
		BaseResultModel baseResultModel = new BaseResultModel();

		//获取用户所绑定的参数
		ShiroSessionData shiroSessionData = ShiroSessionDataUtil.getShiroSessionData();
		dto.setCid(shiroSessionData.getCid());
		dto.setShiroSessionData(shiroSessionData);

		//根据获取部门下的全部用户
		List<SysUser> list = accountRpcService.findListByDto(dto);
		for(int i=0;i<list.size();i++) {
			SysUserRoleDto sysUserRoleDto = new SysUserRoleDto();
			sysUserRoleDto.setUserId(list.get(i).getId());
			List<SysUserRole> sysUserRoles = accountRpcService.selectSysUserRoles(sysUserRoleDto);
			//用于记录用户对应的所有角色
			String roleNames = "";
			for(int o=0;o<sysUserRoles.size();o++){
				if(o == 0){
					roleNames += sysUserRoles.get(o).getRoleName();
				}else {
					roleNames += "," + sysUserRoles.get(o).getRoleName();
				}
			}
			list.get(i).setRoleName(roleNames);

			//用于标记查询出的用户哪一个是当前用户
			if(shiroSessionData.getUserId().equals(list.get(i).getId())) {
				//标记当前用户
				list.get(i).setCurrentUser(1);
			}else {
				//标记非当前用户
				list.get(i).setCurrentUser(0);
			}
		}
		if(list != null && list.size() > 0) {
			baseResultModel.setData(list);
			baseResultModel.setStatus(ResultStatus.SUCCESS.getStatusCode());
			baseResultModel.setMessage(ResultStatus.SUCCESS.getStatusMessage());
		}else {
			baseResultModel.setStatus(ResultStatus.NO_RECORD.getStatusCode());
			baseResultModel.setMessage(ResultStatus.NO_RECORD.getStatusMessage());
		}
		return baseResultModel;
	}

	/**
	 * @description: 根据用户名和手机号模糊查询全部用户信息，按部门分类
	 *
	 * @return
	 * @author ZhangHao
	 * @date 2018年5月25日 下午5:44:48
	 * @version 1.0.0.1
	 */
	@Override
	public Object ajaxSelectUserByKeyWord(SysUserDto dto) {
		BaseResultModel baseResultModel = new BaseResultModel();
		//获取用户所绑定的参数
		ShiroSessionData shiroSessionData = ShiroSessionDataUtil.getShiroSessionData();
		dto.setCid(shiroSessionData.getCid());
		dto.setShiroSessionData(shiroSessionData);
		//查询全部的部门
		CompanyDepartmentDto companyDepartmentDto = new CompanyDepartmentDto();
		companyDepartmentDto.setCid(shiroSessionData.getCid());
		List<CompanyDepartment> departments = departmentRpcService.findListByDto(companyDepartmentDto);

		//根据关键词模糊查询用户
		List<SysUser> list = accountRpcService.findListByDto(dto);
		//循环全部用户，递归查询部门名称进行拼装
		for(int i=0;i<list.size();i++) {

			/** 改段用于获取用户的一组角色*/
			SysUserRoleDto sysUserRoleDto = new SysUserRoleDto();
			sysUserRoleDto.setUserId(list.get(i).getId());
			List<SysUserRole> sysUserRoles = accountRpcService.selectSysUserRoles(sysUserRoleDto);
			//用于记录用户对应的所有角色
			String roleNames = "";
			for(int o=0;o<sysUserRoles.size();o++){
				if(o == 0){
					roleNames += sysUserRoles.get(o).getRoleName();
				}else {
					roleNames += "," + sysUserRoles.get(o).getRoleName();
				}

				//用于标记查询出的用户哪一个是当前用户
				if(shiroSessionData.getUserId().equals(list.get(i).getId())) {
					//标记当前用户
					list.get(i).setCurrentUser(1);
				}else {
					//标记非当前用户
					list.get(i).setCurrentUser(0);
				}
			}
			list.get(i).setRoleName(roleNames);


			/** 该段用于获取用户的一组上级部门*/
			String departMentType = "";
			//递归查询所有上级部门
			List<CompanyDepartment> parentDepartments = new ArrayList<CompanyDepartment>();

			dgCategoryList(parentDepartments,departments,String.valueOf(list.get(i).getDepartmentId()));

			//如果没有父级部门，直接显示本部门
			if(parentDepartments == null || parentDepartments.size() == 0) {
				departMentType = getDepartmentNameById(departments,list.get(i).getDepartmentId());
			}else {
				for(int o=0;o<parentDepartments.size();o++) {
					//父部门只有一个，所以这里直接get(0)就好了
					departMentType = parentDepartments.get(o).getName() + "一" + departMentType ;
				}
				if(departMentType.length() > 0) {
					departMentType = departMentType.substring(0, departMentType.length() - 1);
				}

			}
			list.get(i).setDepartmentName(departMentType);
		}
		if(list != null && list.size() > 0) {
			baseResultModel.setData(list);
			baseResultModel.setStatus(ResultStatus.SUCCESS.getStatusCode());
			baseResultModel.setMessage(ResultStatus.SUCCESS.getStatusMessage());
		}else {
			baseResultModel.setStatus(ResultStatus.NO_RECORD.getStatusCode());
			baseResultModel.setMessage(ResultStatus.NO_RECORD.getStatusMessage());
		}
		return baseResultModel;
	}



	/**
	 * @description:  递归方法(部门递归,查询全部上级部门)
	 *
	 * @param
	 * @author ZhangHao
	 * @date 2018/10/29 10:45
	 * @version 1.0.0.1
	 * @return void
	 */

	@SuppressWarnings("unused")
	private void dgCategoryList(List<CompanyDepartment> parentList,List<CompanyDepartment> list, String myId){
		//循环全部的部门
		for (CompanyDepartment c : list){
			if (null != c && null != c.getId() && null != c.getParentDeptId()){
				//拿到每个部门的id和父id
				String id = String.valueOf(c.getId());
				String pid = String.valueOf(c.getParentDeptId());
				//如果当前id与循环中的id相同
				if (myId.equals(id)){
					//将每一轮查询出的父节点放入集合
					//c.setCompanyDepartmentList(parents);
					parentList.add(c);
					//将父节点的id传入递归方法
					dgCategoryList(parentList,list, pid);
				}
			}
		}
	}


	/**
	 * @description: 根据部门id查询部门名称
	 *
	 * @param list
	 * @param myId
	 * @return
	 * @author ZhangHao
	 * @date 2018年5月25日 下午8:09:40
	 * @version 1.0.0.1
	 */
	@SuppressWarnings("unused")
	private String getDepartmentNameById(List<CompanyDepartment> list,Long myId) {
		String name = "";
		for(int i=0;i<list.size();i++) {
			if(myId.equals(list.get(i).getId())) {
				name = list.get(i).getName();
				break;
			}
		}
		return name;
	}

	/**
	 * @description: 根据状态操作用户 0启用 1停用 2删除
	 *
	 * @param dto
	 * @return
	 * @author ZhangHao
	 * @date 2018年5月26日 上午11:53:01
	 * @version 1.0.0.1
	 */
	@Override
	public Object ajaxOptionUser(SysUserDto dto) {
		BaseResultModel baseResultModel = new BaseResultModel();
		SysUser sysUser = new SysUser();
		sysUser.setId(dto.getId());
		sysUser.setStatus(dto.getStatus());
		sysUser.setUpdateTime(new Date());
		Integer u = 0;
		String sendSmsInfo = "";
		String codeType = "";
		//如果是启用或者是停用
		if(dto.getStatus() == 0) {
			u = accountRpcService.updateSelective(sysUser);
			codeType = "operateQy_code";
			sendSmsInfo = "您的账号已启用，可以正常登录使用";
		}else if(dto.getStatus() == 1) {
			sysUser.setStopTime(new Date());
			u = accountRpcService.updateSelective(sysUser);
			sendSmsInfo = "您的账号已停用，不可以正常登录使用";
			codeType = "operateTy_code";
		}else if(dto.getStatus() == 2) {
			sysUser.setStatus(1);
			sysUser.setIsDeleted(1);
			u = accountRpcService.updateSelective(sysUser);
		}

		//如果更新成功
		if(u > 0) {
			baseResultModel.setStatus(ResultStatus.SUCCESS.getStatusCode());
			baseResultModel.setMessage(ResultStatus.SUCCESS.getStatusMessage());
		}else {
			baseResultModel.setStatus(ResultStatus.NO_RECORD.getStatusCode());
			baseResultModel.setMessage(ResultStatus.NO_RECORD.getStatusMessage());
		}
		return baseResultModel;
	}


	/**
	 * @description: 去数据库为一组用户绑定一组角色
	 *
	 * @param dto
	 * @return
	 * @author ZhangHao
	 * @date 2018年5月26日 下午5:15:09
	 * @version 1.0.0.1
	 */
	@Override
	public Object ajaxCreateUserRolesGroup(SysUserDto dto) {
		BaseResultModel baseResultModel = new BaseResultModel();
		//获取用户所绑定的参数
		ShiroSessionData shiroSessionData = ShiroSessionDataUtil.getShiroSessionData();

		String roleIds  = dto.getRoleIds();
		String userIds = dto.getUserIds();
		Integer tag = 0;
		//根据id将用户角色中间表中的对应关系全部更新为is_deleted
		String [] userIdsArr = userIds.split(",");
		String [] roleIdsArr = roleIds.split(",");
		for(int i=0;i<userIdsArr.length;i++) {
			SysUserRoleDto sysUserRoleDto = new SysUserRoleDto();
			sysUserRoleDto.setUserId(Long.valueOf(userIdsArr[i]));
			sysUserRoleDto.setUpdateTime(new Date());
			sysUserRoleDto.setIsDeleted(1);
			//更新该用户所有角色对应关系为删除状态
			accountRpcService.updateSysUserRolesByType(sysUserRoleDto);

			//crm外部权限
			Set<String> crmRoles = new HashSet<String>();
			//app权限
			Set<String> appRoles = new HashSet<String>();

			//循环角色，增加用户和角色的对应关系
			for(int o=0;o<roleIdsArr.length;o++) {
				SysUserRole entity = new SysUserRole();
				entity.setUserId(Long.valueOf(userIdsArr[i]));
				entity.setRoleId(Long.valueOf(roleIdsArr[o]));
				entity.setIsDeleted(0);
				entity.setCreateUserId(shiroSessionData.getUserId());
				SysUserRole sysUserRole = accountRpcService.insertUserRoles(entity);
				if(sysUserRole != null) {
					tag = tag + 1;
				}

				//根据角色id查询该角色对应的外部CRM权限，由于用户所对应的角色不会特别多，所以此处暂时查库，如有问题后续优化，查询出全量数据进行匹配
				SysRoleDto sysRoleSelectDto = new SysRoleDto();
				sysRoleSelectDto.setId(Long.valueOf(roleIdsArr[o]));
				sysRoleSelectDto.setCid(shiroSessionData.getCid());
				SysRole sysRole = roleRpcService.selectSysRoleByType(sysRoleSelectDto);
				//获取CRM外部权限，按逗号拆分取第一条就是crm权限
				String crmRoleId = "";
				//获取用户选择的app权限,按逗号拆分第二条就是app权限
				String appRoleId = "";
				if(sysRole.getExternalAuthority() != null && !"".equals(sysRole.getExternalAuthority())) {
					crmRoleId  = String.valueOf(sysRole.getExternalAuthority().split(",")[0]);
					//说明用户选择了门店APP
					if(sysRole.getExternalAuthority().split(",").length == 2) {
						appRoleId	=  String.valueOf(sysRole.getExternalAuthority().split(",")[1]);
					}
				}
				//拿到不重复的crm角色id
				if(!"".equals(crmRoleId)) {
					crmRoles.add(crmRoleId);
				}
				//拿到不重复的app角色Id
				if(!"".equals(appRoleId)) {
					appRoles.add(appRoleId);
				}
			}

			/** 同步CRM逻辑开始**/
			//转换成 有序集合 CRM角色
			List<String> listCrmRoles = new ArrayList<String>(crmRoles);
			//转换成 有序集合 APP角色
			List<String> listAppRoles = new ArrayList<String>(appRoles);
			//最终要同步给crm的Id
			String synCrmRoleId = "";
			//最终要更新用户对应的app权限Id
			String updateAppRoleId = "";
			//遍历所有crm角色id
			for(int p=0;p<listCrmRoles.size();p++) {
				//如果是品牌管理员
				if("1".equals(listCrmRoles.get(p))) {
					synCrmRoleId = listCrmRoles.get(p);
					break;
					//如果是店长
				}else if("2".equals(listCrmRoles.get(p))) {
					synCrmRoleId = listCrmRoles.get(p);
					break;
					//如果是店员
				}else if("3".equals(listCrmRoles.get(p))) {
					synCrmRoleId = listCrmRoles.get(p);
					break;
				}
			}
			//遍历所有app角色id
			for(int a=0;a<listAppRoles.size();a++) {
				//如果是店长
				if("0".equals(listAppRoles.get(a))) {
					updateAppRoleId = listAppRoles.get(a);
					break;
				}
				//如果是店员
				if("1".equals(listAppRoles.get(a))) {
					updateAppRoleId = listAppRoles.get(a);
					break;
				}
			}
			//最终要更新用户对应的app权限标识
			if(!"".equals(updateAppRoleId)) {
				SysUser sysUser = new SysUser();
				sysUser.setId(Long.valueOf(userIdsArr[i]));
				sysUser.setUpdateTime(new Date());
				sysUser.setAppIdentity(Integer.valueOf(updateAppRoleId));
				accountRpcService.updateSelective(sysUser);
			}
			//如果最终要同步给crm的角色id不为空，则说明要为该用户开通crm权限
			if(!"".equals(synCrmRoleId)) {

				//1、根据公司id查询该公司下的全部crm角色
				SysRoleDto sysRoleCrmDto = new SysRoleDto();
				sysRoleCrmDto.setCid(shiroSessionData.getCid());
				sysRoleCrmDto.setProductId(1l);
				List<SysRole> sysCrmRoles = roleRpcService.findListByDto(sysRoleCrmDto);

				//2、根据crm角色id 匹配crm角色酷客ID结果集，拿到要为用户分配的crm酷客角色ID
				for(int a=0;a<sysCrmRoles.size();a++) {
					//3、增加当前用户和该crm酷客角色的对应关系
					if(synCrmRoleId.equals(String.valueOf(sysCrmRoles.get(a).getProductRoleId()))) {
						SysUserRole sysUserRole = new SysUserRole();
						sysUserRole.setUserId(Long.valueOf(userIdsArr[i]));
						sysUserRole.setRoleId(Long.valueOf(sysCrmRoles.get(a).getId()));
						accountRpcService.insertUserRoles(sysUserRole);
					}
				}
				//查询用户对应的基础信息
				SysUserDto sudto = new SysUserDto();
				sudto.setId(Long.valueOf(userIdsArr[i]));
				sudto.setCid(shiroSessionData.getCid());
				SysUser sysu = accountRpcService.findUserInfoByType(sudto);
				//查询用户所绑定的门店
				BaseStoreToUserDto baseStoreToUserDto = new BaseStoreToUserDto();
				baseStoreToUserDto.setUserId(Long.valueOf(userIdsArr[i]));
				baseStoreToUserDto.setCid(shiroSessionData.getCid());
				List<BaseStoreToUser> baseStoreToUsers = accountRpcService.selectUserStores(baseStoreToUserDto);

			}

			/** 同步CRM逻辑结束**/


			/** 根据用户ID查询用户，调用接口进行踢出**/
			SysUserDto sysUserDto = new SysUserDto();
			sysUserDto.setId(Long.valueOf(userIdsArr[i]));
			SysUser sysUser = accountRpcService.findUserInfoByType(sysUserDto);
			//踢出配置角色后的用户
			if(sysUser != null) {
				loginService.KickOutTheOnlineUser(sysUser.getThirdToken(),String.valueOf(sysUser.getId()));
			}

		}
		if(tag > 0) {
			baseResultModel.setStatus(ResultStatus.SUCCESS.getStatusCode());
			baseResultModel.setMessage(ResultStatus.SUCCESS.getStatusMessage());
		}else {
			baseResultModel.setStatus(ResultStatus.NO_RECORD.getStatusCode());
			baseResultModel.setMessage(ResultStatus.NO_RECORD.getStatusMessage());
		}
		return baseResultModel;
	}


	/**
	 * @description: 重新赋予CRM角色
	 *
	 * @param dto
	 * @return
	 * @author ZhangHao
	 * @date 2018年5月26日 下午5:15:09
	 * @version 1.0.0.1
	 */
	@Override
	public Object ajaxCreateUserRolesGroupForCrm(SysUserDto dto) {
		BaseResultModel baseResultModel = new BaseResultModel();
		//获取用户所绑定的参数

		String roleIds  = dto.getRoleIds();
		String userIds = dto.getUserIds();
		//根据id将用户角色中间表中的对应关系全部更新为is_deleted
		String [] userIdsArr = userIds.split(",");
		String [] roleIdsArr = roleIds.split(",");
		for(int i=0;i<userIdsArr.length;i++) {

			//crm外部权限
			Set<String> crmRoles = new HashSet<String>();
			//app权限
			Set<String> appRoles = new HashSet<String>();

			for(int o=0;o<roleIdsArr.length;o++) {
				//根据角色id查询该角色对应的外部CRM权限，由于用户所对应的角色不会特别多，所以此处暂时查库，如有问题后续优化，查询出全量数据进行匹配
				SysRoleDto sysRoleSelectDto = new SysRoleDto();
				sysRoleSelectDto.setId(Long.valueOf(roleIdsArr[o]));
				sysRoleSelectDto.setCid(dto.getCid());
				SysRole sysRole = roleRpcService.selectSysRoleByType(sysRoleSelectDto);
				//获取CRM外部权限，按逗号拆分取第一条就是crm权限
				String crmRoleId = "";
				//获取用户选择的app权限,按逗号拆分第二条就是app权限
				String appRoleId = "";
				if(sysRole.getExternalAuthority() != null && !"".equals(sysRole.getExternalAuthority())) {
					crmRoleId  = String.valueOf(sysRole.getExternalAuthority().split(",")[0]);
					//说明用户选择了门店APP
					if(sysRole.getExternalAuthority().split(",").length == 2) {
						appRoleId	=  String.valueOf(sysRole.getExternalAuthority().split(",")[1]);
					}
				}
				//拿到不重复的crm角色id
				if(!"".equals(crmRoleId)) {
					crmRoles.add(crmRoleId);
				}
				//拿到不重复的app角色Id
				if(!"".equals(appRoleId)) {
					appRoles.add(appRoleId);
				}
			}

			/** 同步CRM逻辑开始**/
			//转换成 有序集合 CRM角色
			List<String> listCrmRoles = new ArrayList<String>(crmRoles);
			//转换成 有序集合 APP角色
			List<String> listAppRoles = new ArrayList<String>(appRoles);
			//最终要同步给crm的Id
			String synCrmRoleId = "";
			//最终要更新用户对应的app权限Id
			String updateAppRoleId = "";
			//遍历所有crm角色id
			for(int p=0;p<listCrmRoles.size();p++) {
				//如果是品牌管理员
				if("1".equals(listCrmRoles.get(p))) {
					synCrmRoleId = listCrmRoles.get(p);
					break;
					//如果是店长
				}else if("2".equals(listCrmRoles.get(p))) {
					synCrmRoleId = listCrmRoles.get(p);
					break;
					//如果是店员
				}else if("3".equals(listCrmRoles.get(p))) {
					synCrmRoleId = listCrmRoles.get(p);
					break;
				}
			}
			//遍历所有app角色id
			for(int a=0;a<listAppRoles.size();a++) {
				//如果是店长
				if("0".equals(listAppRoles.get(a))) {
					updateAppRoleId = listAppRoles.get(a);
					break;
				}
				//如果是店员
				if("1".equals(listAppRoles.get(a))) {
					updateAppRoleId = listAppRoles.get(a);
					break;
				}
			}
			//最终要更新用户对应的app权限标识
			if(!"".equals(updateAppRoleId)) {
				SysUser sysUser = new SysUser();
				sysUser.setId(Long.valueOf(userIdsArr[i]));
				sysUser.setUpdateTime(new Date());
				sysUser.setAppIdentity(Integer.valueOf(updateAppRoleId));
				accountRpcService.updateSelective(sysUser);
			}
			//如果最终要同步给crm的角色id不为空，则说明要为该用户开通crm权限
			if(!"".equals(synCrmRoleId)) {

				//1、根据公司id查询该公司下的全部crm角色
				SysRoleDto sysRoleCrmDto = new SysRoleDto();
				sysRoleCrmDto.setCid(dto.getCid());
				sysRoleCrmDto.setProductId(1l);
				List<SysRole> sysCrmRoles = roleRpcService.findListByDto(sysRoleCrmDto);

				//2、将该用户对应的全部CRM角色去除
				for(int s=0;s<sysCrmRoles.size();s++) {
					SysUserRoleDto sysUserRoleDto = new SysUserRoleDto();
					sysUserRoleDto.setIsDeleted(1);
					sysUserRoleDto.setUserId(Long.valueOf(userIdsArr[i]));
					sysUserRoleDto.setRoleId(sysCrmRoles.get(s).getId());
					sysUserRoleDto.setUpdateTime(new Date());
					accountRpcService.updateSysUserRolesByType(sysUserRoleDto);

				}


				//3、根据crm角色id 匹配crm角色酷客ID结果集，拿到要为用户分配的crm酷客角色ID
				for(int a=0;a<sysCrmRoles.size();a++) {
					//3、增加当前用户和该crm酷客角色的对应关系
					if(synCrmRoleId.equals(String.valueOf(sysCrmRoles.get(a).getProductRoleId()))) {
						SysUserRole sysUserRole = new SysUserRole();
						sysUserRole.setUserId(Long.valueOf(userIdsArr[i]));
						sysUserRole.setRoleId(Long.valueOf(sysCrmRoles.get(a).getId()));
						accountRpcService.insertUserRoles(sysUserRole);
					}
				}
				//查询用户对应的基础信息
				SysUserDto sudto = new SysUserDto();
				sudto.setId(Long.valueOf(userIdsArr[i]));
				sudto.setCid(dto.getCid());
				SysUser sysu = accountRpcService.findUserInfoByType(sudto);
				//查询用户所绑定的门店
				BaseStoreToUserDto baseStoreToUserDto = new BaseStoreToUserDto();
				baseStoreToUserDto.setUserId(Long.valueOf(userIdsArr[i]));
				baseStoreToUserDto.setCid(dto.getCid());
				List<BaseStoreToUser> baseStoreToUsers = accountRpcService.selectUserStores(baseStoreToUserDto);

			}

			/** 同步CRM逻辑结束**/

			/** 根据用户ID查询用户，调用接口进行踢出**/
			SysUserDto sysUserDto = new SysUserDto();
			sysUserDto.setId(Long.valueOf(userIdsArr[i]));
			SysUser sysUser = accountRpcService.findUserInfoByType(sysUserDto);
			if(sysUser != null) {
				//踢出角色修改后，该角色绑定的用户
				loginService.KickOutTheOnlineUser(sysUser.getThirdToken(),String.valueOf(sysUser.getId()));
			}
		}

		baseResultModel.setStatus(ResultStatus.SUCCESS.getStatusCode());
		baseResultModel.setMessage(ResultStatus.SUCCESS.getStatusMessage());
		return baseResultModel;
	}


	/**
	 * @description: 创建用户及用户绑定的相关信息
	 *
	 * @return
	 * @author ZhangHao
	 * @date 2018年5月27日 上午8:10:47
	 * @version 1.0.0.1
	 */
	@Override
	public Object ajaxCreateUser(SysUser entity) {

		//用户所属部门、用户身份标识、用户名、手机号、邮箱、员工编号,已用实体接收

		BaseResultModel baseResultModel = new BaseResultModel();
		//获取用户所绑定的参数
		ShiroSessionData shiroSessionData = ShiroSessionDataUtil.getShiroSessionData();

		//用户负责的一组门店
		String responsibleStoreIds =  entity.getResponsibleStoreIds();
		//用户的所属门店
		Long storeId = entity.getStoreId();
		//用户负责的一组部门
		String responsibleDepartmentIds = entity.getResponsibleDepartmentIds();
		//用户绑定的一组角色
		String roleIds = entity.getRoleIds();


		//根据手机号判断该用户是否存在
		SysUserDto dto = new SysUserDto();
		dto.setAccount(entity.getAccount());
		SysUser rdata = accountRpcService.findUserInfoByType(dto);
		if(rdata != null) {
			baseResultModel.setStatus(ResultStatus.NO_RECORD.getStatusCode());
			baseResultModel.setMessage("该手机号已被使用");
			return baseResultModel;
		}

		entity.setCid(shiroSessionData.getCid());
		//设置6位随机密码
		String code="";
		String codeMd5 = "";
		Random rand=new Random();//生成随机数
		for(int a=0;a<6;a++){
			code+=rand.nextInt(10);//生成6位随机密码
		}
		//密码二次加密
		codeMd5 = MD5Util.encodeString(MD5Util.encodeString(code) + ConfigUtil.MD5_PWD_STR);
		entity.setPwd(codeMd5);
		//设置首次登陆状态为0
		entity.setIsFirstLogin(0);
		//用户信息入库
		entity = accountRpcService.insertSelective(entity);
		//如果入库成功，增加用户的绑定关系
		if(entity != null) {
			//根据属性名称查询属性对应的id
			SysAttributeDto sysAttributeDto = new SysAttributeDto();
			sysAttributeDto.setCid(shiroSessionData.getCid());
			sysAttributeDto.setAttributeName("store");
			SysAttribute sysAttribute = roleRpcService.findSysAttributeByType(sysAttributeDto);
			//为用户增加用户数据权限
			SysUserAttribute sysUserAttribute = new SysUserAttribute();
			sysUserAttribute.setIsDeleted(0);
			sysUserAttribute.setCreateUserId(shiroSessionData.getUserId());
			sysUserAttribute.setCid(shiroSessionData.getCid());
			sysUserAttribute.setAttributeValue(responsibleStoreIds);
			sysUserAttribute.setAttributeId(sysAttribute.getId());
			sysUserAttribute.setUserId(entity.getId());
			accountRpcService.inserttUserAttribute(sysUserAttribute);

			//根据属性名称查询属性对应的id
			SysAttributeDto sysAttributeDtoForDepartment = new SysAttributeDto();
			sysAttributeDtoForDepartment.setCid(shiroSessionData.getCid());
			sysAttributeDtoForDepartment.setAttributeName("department");
			SysAttribute sysAttributeDepartment = roleRpcService.findSysAttributeByType(sysAttributeDtoForDepartment);

			//为用户增加部门数据权限
			sysUserAttribute.setIsDeleted(0);
			sysUserAttribute.setCreateUserId(shiroSessionData.getUserId());
			sysUserAttribute.setCid(shiroSessionData.getCid());
			sysUserAttribute.setAttributeValue(responsibleDepartmentIds);
			sysUserAttribute.setAttributeId(sysAttributeDepartment.getId());
			sysUserAttribute.setUserId(entity.getId());
			accountRpcService.inserttUserAttribute(sysUserAttribute);

			//增加用户和门店的对应关系
			BaseStoreToUser baseStoreToUser = new BaseStoreToUser();
			baseStoreToUser.setIsDeleted(0);
			baseStoreToUser.setCid(shiroSessionData.getCid());
			baseStoreToUser.setCreateUserId(shiroSessionData.getUserId());
			baseStoreToUser.setUserId(entity.getId());
			baseStoreToUser.setStoreId(storeId);
			accountRpcService.insertBaseStoreToUser(baseStoreToUser);

			if(!"".equals(roleIds) && roleIds != null) {

				//增加用户对应的一组角色
				String [] roleIdsArr = roleIds.split(",");

				//crm外部权限
				Set<String> crmRoles = new HashSet<String>();
				//app权限
				Set<String> appRoles = new HashSet<String>();

				//循环角色，增加用户和角色的对应关系
				for(int o=0;o<roleIdsArr.length;o++) {
					//角色id
					Long roleId = Long.valueOf(roleIdsArr[o]);
					//入库用户和角色的对应关系
					SysUserRole sysUserRole = new SysUserRole();
					sysUserRole.setUserId(entity.getId());
					sysUserRole.setRoleId(roleId);
					sysUserRole.setIsDeleted(0);
					sysUserRole.setCreateUserId(shiroSessionData.getUserId());
					accountRpcService.insertUserRoles(sysUserRole);

					//根据角色id查询该角色对应的外部CRM权限，由于用户所对应的角色不会特别多，所以此处暂时查库，如有问题后续优化，查询出全量数据进行匹配
					SysRoleDto sysRoleDto = new SysRoleDto();
					sysRoleDto.setId(roleId);
					sysRoleDto.setCid(shiroSessionData.getCid());
					SysRole sysRole = roleRpcService.selectSysRoleByType(sysRoleDto);
					//获取CRM外部权限，按逗号拆分取第一条就是crm权限
					String crmRoleId = "";
					//获取用户选择的app权限,按逗号拆分第二条就是app权限
					String appRoleId = "";
					if(sysRole.getExternalAuthority() != null && !"".equals(sysRole.getExternalAuthority())) {
						crmRoleId  = String.valueOf(sysRole.getExternalAuthority().split(",")[0]);
						//说明用户选择了门店APP
						if(sysRole.getExternalAuthority().split(",").length == 2) {
							appRoleId	=  String.valueOf(sysRole.getExternalAuthority().split(",")[1]);
						}
					}
					//拿到不重复的crm角色id
					if(!"".equals(crmRoleId)) {
						crmRoles.add(crmRoleId);
					}
					//拿到不重复的app角色id
					if(!"".equals(appRoleId)) {
						appRoles.add(appRoleId);
					}
				}
				/** 同步CRM逻辑开始**/
				//转换成 有序集合 CRM角色
				List<String> listCrmRoles = new ArrayList<String>(crmRoles);
				//转换成 有序集合 APP角色
				List<String> listAppRoles = new ArrayList<String>(appRoles);
				//最终要同步给crm的Id
				String synCrmRoleId = "";
				//最终要更新用户对应的app权限Id
				String updateAppRoleId = "";
				//遍历所有crm角色id
				for(int i=0;i<listCrmRoles.size();i++) {
					//如果是品牌管理员
					if("1".equals(listCrmRoles.get(i))) {
						synCrmRoleId = listCrmRoles.get(i);
						break;
						//如果是店长
					}else if("2".equals(listCrmRoles.get(i))) {
						synCrmRoleId = listCrmRoles.get(i);
						break;
						//如果是店员
					}else if("3".equals(listCrmRoles.get(i))) {
						synCrmRoleId = listCrmRoles.get(i);
						break;
					}
				}
				//遍历所有app角色id
				for(int i=0;i<listAppRoles.size();i++) {
					//如果是店长
					if("0".equals(listAppRoles.get(i))) {
						updateAppRoleId = listAppRoles.get(i);
						break;
					}
					//如果是店员
					if("1".equals(listAppRoles.get(i))) {
						updateAppRoleId = listAppRoles.get(i);
						break;
					}
				}
				//最终要更新用户对应的app权限标识
				if(!"".equals(updateAppRoleId)) {
					SysUser sysUser = new SysUser();
					sysUser.setId(entity.getId());
					sysUser.setUpdateTime(new Date());
					sysUser.setAppIdentity(Integer.valueOf(updateAppRoleId));
					accountRpcService.updateSelective(sysUser);
				}

				//如果最终要同步给crm的角色id不为空，则说明要为该用户开通crm权限
				if(!"".equals(synCrmRoleId)) {

					//1、根据公司id查询该公司下的全部crm角色
					SysRoleDto sysRoleDto = new SysRoleDto();
					sysRoleDto.setCid(shiroSessionData.getCid());
					List<SysRole> sysRoles = roleRpcService.findListByDto(sysRoleDto);

					//2、根据crm角色id 匹配crm角色酷客ID结果集，拿到要为用户分配的crm酷客角色ID
					for(int i=0;i<sysRoles.size();i++) {
						//3、增加当前用户和该crm酷客角色的对应关系
						if(synCrmRoleId.equals(String.valueOf(sysRoles.get(i).getProductRoleId()))) {
							SysUserRole sysUserRole = new SysUserRole();
							sysUserRole.setUserId(entity.getId());
							sysUserRole.setRoleId(Long.valueOf(sysRoles.get(i).getId()));
							accountRpcService.insertUserRoles(sysUserRole);
						}
					}
				}

				/** 同步CRM逻辑结束**/
			}

			baseResultModel.setStatus(ResultStatus.SUCCESS.getStatusCode());
			baseResultModel.setMessage(ResultStatus.SUCCESS.getStatusMessage());
			return baseResultModel;
		}else {
			baseResultModel.setStatus(ResultStatus.NO_RECORD.getStatusCode());
			baseResultModel.setMessage("创建用户失败");
			return baseResultModel;
		}

	}

	/**
	 * @description: 初始化用户数据，用于编辑员工模块回显数据
	 *
	 * @return
	 * @author ZhangHao
	 * @date 2018年5月27日 上午11:19:10
	 * @version 1.0.0.1
	 */
	@Override
	public Object ajaxSeleUserInfoForUpdate(SysUserDto dto) {

		BaseResultModel baseResultModel = new BaseResultModel();
		//获取用户所绑定的参数
		ShiroSessionData shiroSessionData = ShiroSessionDataUtil.getShiroSessionData();
		dto.setCid(shiroSessionData.getCid());
		//用户基本信息
		SysUser sysUser = accountRpcService.findUserInfoByType(dto);

		//需额外查询字段
		//用户负责的一组门店
		//用户负责的一组部门
		List<SysUser> list = accountRpcService.findUserAttributeByDto(dto);
		for(int i=0;i<list.size();i++) {
			if("store".equals(list.get(i).getAttributeName())) {
				sysUser.setResponsibleStoreIds(list.get(i).getAttributeValue());
			}else if("department".equals(list.get(i).getAttributeName())) {
				sysUser.setResponsibleDepartmentIds(list.get(i).getAttributeValue());
			}
		}
		BaseStoreToUserDto baseStoreToUserDto = new BaseStoreToUserDto();
		baseStoreToUserDto.setCid(shiroSessionData.getCid());
		baseStoreToUserDto.setUserId(dto.getId());
		List<BaseStoreToUser> listStores = accountRpcService.selectUserStores(baseStoreToUserDto);
		//用户所属门店
		String storesStr = "";

		for(int i=0;i<listStores.size();i++) {
			if(i == 0) {
				storesStr += listStores.get(i).getStoreId();
			}
			//else {
			//	storesStr += "," + listStores.get(i).getStoreId();
			//}
		}
		if(!"".equals(storesStr)) {
			sysUser.setStoreId(Long.valueOf(storesStr));
		}
		//用户对应的一组角色
		SysUserRoleDto sysUserRoleDto = new SysUserRoleDto();
		sysUserRoleDto.setUserId(dto.getId());
		List<SysUserRole> listRoles = accountRpcService.selectSysUserRoles(sysUserRoleDto);
		String rolesStr = "";
		for(int i=0;i<listRoles.size();i++) {
			if(i == 0) {
				rolesStr += listRoles.get(i).getRoleId();
			}else {
				rolesStr += "," +listRoles.get(i).getRoleId();
			}
		}
		sysUser.setRoleIds(rolesStr);
		baseResultModel.setData(sysUser);
		baseResultModel.setStatus(ResultStatus.SUCCESS.getStatusCode());
		baseResultModel.setMessage(ResultStatus.SUCCESS.getStatusMessage());
		return baseResultModel;
	}

	/**
	 * @description: 编辑 用户信息
	 *
	 * @param entity
	 * @return
	 * @author ZhangHao
	 * @date 2018年5月27日 下午1:39:51
	 * @version 1.0.0.1
	 */
	@Override
	public Object ajaxUpdateUser(SysUser entity) {

		//用户所属部门、用户身份标识、用户名、手机号、邮箱、员工编号,已用实体接收

		BaseResultModel baseResultModel = new BaseResultModel();
		//获取用户所绑定的参数
		ShiroSessionData shiroSessionData = ShiroSessionDataUtil.getShiroSessionData();

		//用户负责的一组门店
		String responsibleStoreIds =  entity.getResponsibleStoreIds();
		//用户的所属门店
		Long storeId = entity.getStoreId();
		//用户负责的一组部门
		String responsibleDepartmentIds = entity.getResponsibleDepartmentIds();
		//用户绑定的一组角色
		String roleIds = entity.getRoleIds();

		entity.setCid(shiroSessionData.getCid());
		entity.setUpdateTime(new Date());
		//用户信息编辑
		accountRpcService.updateSelective(entity);


		//根据用户id逻辑删除所有的数据权限
		SysUserAttribute suai = new SysUserAttribute();
		suai.setUserId(entity.getId());
		suai.setUpdateTime(new Date());
		suai.setIsDeleted(1);
		accountRpcService.updateUserAttribute(suai);

		//根据属性名称查询属性对应的id
		SysAttributeDto sysAttributeDto = new SysAttributeDto();
		sysAttributeDto.setCid(shiroSessionData.getCid());
		sysAttributeDto.setAttributeName("store");
		SysAttribute sysAttribute = roleRpcService.findSysAttributeByType(sysAttributeDto);
		//为用户增加用户数据权限
		SysUserAttribute sysUserAttribute = new SysUserAttribute();
		sysUserAttribute.setIsDeleted(0);
		sysUserAttribute.setCreateUserId(shiroSessionData.getUserId());
		sysUserAttribute.setCid(shiroSessionData.getCid());
		sysUserAttribute.setAttributeValue(responsibleStoreIds);
		sysUserAttribute.setAttributeId(sysAttribute.getId());
		sysUserAttribute.setUserId(entity.getId());
		accountRpcService.inserttUserAttribute(sysUserAttribute);

		//根据属性名称查询属性对应的id
		SysAttributeDto sysAttributeDtoForDepartment = new SysAttributeDto();
		sysAttributeDtoForDepartment.setCid(shiroSessionData.getCid());
		sysAttributeDtoForDepartment.setAttributeName("department");
		SysAttribute sysAttributeDepartment = roleRpcService.findSysAttributeByType(sysAttributeDtoForDepartment);

		//为用户增加部门数据权限
		sysUserAttribute.setIsDeleted(0);
		sysUserAttribute.setCreateUserId(shiroSessionData.getUserId());
		sysUserAttribute.setCid(shiroSessionData.getCid());
		sysUserAttribute.setAttributeValue(responsibleDepartmentIds);
		sysUserAttribute.setAttributeId(sysAttributeDepartment.getId());
		sysUserAttribute.setUserId(entity.getId());
		accountRpcService.inserttUserAttribute(sysUserAttribute);


		//根据用户id将该用户和之前门店的对应逻辑清除
		BaseStoreToUserDto bsdto = new BaseStoreToUserDto();
		bsdto.setUpdateTime(new Date());
		bsdto.setIsDeleted(1);
		bsdto.setUserId(entity.getId());
		bsdto.setCid(shiroSessionData.getCid());
		accountRpcService.updateBaseStoreToUserByType(bsdto);

		//增加用户和门店的对应关系
		BaseStoreToUser baseStoreToUser = new BaseStoreToUser();
		baseStoreToUser.setIsDeleted(0);
		baseStoreToUser.setCid(shiroSessionData.getCid());
		baseStoreToUser.setCreateUserId(shiroSessionData.getUserId());
		baseStoreToUser.setUserId(entity.getId());
		baseStoreToUser.setStoreId(storeId);
		accountRpcService.insertBaseStoreToUser(baseStoreToUser);


		SysUserRoleDto sysUserRoleDto = new SysUserRoleDto();
		sysUserRoleDto.setUserId(entity.getId());
		sysUserRoleDto.setUpdateTime(new Date());
		sysUserRoleDto.setIsDeleted(1);
		//更新该用户所有角色对应关系为删除状态
		accountRpcService.updateSysUserRolesByType(sysUserRoleDto);



		if(!"".equals(roleIds) && roleIds != null) {
			//增加用户对应的一组角色
			String [] roleIdsArr = roleIds.split(",");

			//crm外部权限
			Set<String> crmRoles = new HashSet<String>();
			//app权限
			Set<String> appRoles = new HashSet<String>();

			//循环角色，增加用户和角色的对应关系
			for(int o=0;o<roleIdsArr.length;o++) {
				//角色id
				Long roleId = Long.valueOf(roleIdsArr[o]);
				//入库用户和角色的对应关系
				SysUserRole sysUserRole = new SysUserRole();
				sysUserRole.setUserId(entity.getId());
				sysUserRole.setRoleId(roleId);
				sysUserRole.setIsDeleted(0);
				sysUserRole.setCreateUserId(shiroSessionData.getUserId());
				accountRpcService.insertUserRoles(sysUserRole);

				//根据角色id查询该角色对应的外部CRM权限，由于用户所对应的角色不会特别多，所以此处暂时查库，如有问题后续优化，查询出全量数据进行匹配
				SysRoleDto sysRoleDto = new SysRoleDto();
				sysRoleDto.setId(roleId);
				sysRoleDto.setCid(shiroSessionData.getCid());
				SysRole sysRole = roleRpcService.selectSysRoleByType(sysRoleDto);
				//获取CRM外部权限，按逗号拆分取第一条就是crm权限
				String crmRoleId = "";
				//获取用户选择的app权限,按逗号拆分第二条就是app权限
				String appRoleId = "";
				if(sysRole.getExternalAuthority() != null && !"".equals(sysRole.getExternalAuthority())) {
					crmRoleId  = String.valueOf(sysRole.getExternalAuthority().split(",")[0]);
					//说明用户选择了门店APP
					if(sysRole.getExternalAuthority().split(",").length == 2) {
						appRoleId	=  String.valueOf(sysRole.getExternalAuthority().split(",")[1]);
					}
				}
				//拿到不重复的crm角色id
				if(!"".equals(crmRoleId)) {
					crmRoles.add(crmRoleId);
				}
				//拿到不重复的app角色id
				if(!"".equals(appRoleId)) {
					appRoles.add(appRoleId);
				}

			}
			/** 同步CRM逻辑开始**/
			//转换成 有序集合 CRM角色
			List<String> listCrmRoles = new ArrayList<String>(crmRoles);
			//转换成 有序集合 APP角色
			List<String> listAppRoles = new ArrayList<String>(appRoles);

			//最终要同步给crm的Id
			String synCrmRoleId = "";
			//最终要更新用户对应的app权限Id
			String updateAppRoleId = "";
			//遍历所有crm角色id
			for(int i=0;i<listCrmRoles.size();i++) {
				//如果是品牌管理员
				if("1".equals(listCrmRoles.get(i))) {
					synCrmRoleId = listCrmRoles.get(i);
					break;
					//如果是店长
				}else if("2".equals(listCrmRoles.get(i))) {
					synCrmRoleId = listCrmRoles.get(i);
					break;
					//如果是店员
				}else if("3".equals(listCrmRoles.get(i))) {
					synCrmRoleId = listCrmRoles.get(i);
					break;
				}
			}
			//遍历所有app角色id
			for(int i=0;i<listAppRoles.size();i++) {
				//如果是店长
				if("0".equals(listAppRoles.get(i))) {
					updateAppRoleId = listAppRoles.get(i);
					break;
				}
				//如果是店员
				if("1".equals(listAppRoles.get(i))) {
					updateAppRoleId = listAppRoles.get(i);
					break;
				}
			}
			//最终要更新用户对应的app权限标识
			if(!"".equals(updateAppRoleId)) {
				SysUser sysUser = new SysUser();
				sysUser.setId(entity.getId());
				sysUser.setUpdateTime(new Date());
				sysUser.setAppIdentity(Integer.valueOf(updateAppRoleId));
				accountRpcService.updateSelective(sysUser);
			}

			//如果最终要同步给crm的角色id不为空，则说明要为该用户开通crm权限
			if(!"".equals(synCrmRoleId)) {

				//1、根据公司id查询该公司下的全部crm角色
				SysRoleDto sysRoleDto = new SysRoleDto();
				sysRoleDto.setCid(shiroSessionData.getCid());
				List<SysRole> sysRoles = roleRpcService.findListByDto(sysRoleDto);

				//2、根据crm角色id 匹配crm角色酷客ID结果集，拿到要为用户分配的crm酷客角色ID
				for(int i=0;i<sysRoles.size();i++) {
					//3、增加当前用户和该crm酷客角色的对应关系
					if(synCrmRoleId.equals(String.valueOf(sysRoles.get(i).getProductRoleId()))) {
						SysUserRole sysUserRole = new SysUserRole();
						sysUserRole.setUserId(entity.getId());
						sysUserRole.setRoleId(Long.valueOf(sysRoles.get(i).getId()));
						accountRpcService.insertUserRoles(sysUserRole);
					}
				}
			}

			/** 同步CRM逻辑结束**/
		}


		/** 将当前编辑的用户踢出 **/
		SysUserDto sysUserDto = new SysUserDto();
		sysUserDto.setId(entity.getId());
		SysUser sysUser = accountRpcService.findUserInfoByType(sysUserDto);
		//踢出编辑后的用户
		if(sysUser != null) {
			loginService.KickOutTheOnlineUser(sysUser.getThirdToken(),String.valueOf(sysUser.getId()));
		}
		try {
			baseResultModel.setStatus(ResultStatus.SUCCESS.getStatusCode());
			baseResultModel.setMessage(ResultStatus.SUCCESS.getStatusMessage());
		} catch (Exception e) {
			baseResultModel.setStatus(ResultStatus.NO_RECORD.getStatusCode());
			baseResultModel.setMessage(ResultStatus.NO_RECORD.getStatusMessage());
		}
		return baseResultModel;

	}

	/**
	 * @description: 根据条件定位一个一条记录，判断用户是否存在使用
	 *
	 * @param dto
	 * @return
	 * @author ZhangHao
	 * @date 2018年5月28日 下午12:41:09
	 * @version 1.0.0.1
	 */
	@Override
	public Object ajaxUserInfoByRecoverPwd(SysUserDto dto,HttpServletResponse response) {
		//用户所属部门、用户身份标识、用户名、手机号、邮箱、员工编号,已用实体接收
		BaseResultModel baseResultModel = new BaseResultModel();
		//获取用户所绑定的参数
		ShiroSessionData shiroSessionData = ShiroSessionDataUtil.getShiroSessionData();
		dto.setCid(shiroSessionData.getCid());
		SysUser sysUser = accountRpcService.findUserInfoByType(dto);

		if(sysUser != null) {
			//用户存在，将用户id和account放在cookie中返回
			Cookie accountCK = new Cookie("account", sysUser.getAccount());
			Cookie userIdCK = new Cookie("uid", String.valueOf(sysUser.getId()));
			Cookie tenantIdCK = new Cookie("cid", String.valueOf(sysUser.getCid()));
			Cookie flagCK = new Cookie("flag", "fg");

			accountCK.setPath("/");
			userIdCK.setPath("/");
			tenantIdCK.setPath("/");
			flagCK.setPath("/");

			response.addCookie(accountCK);
			response.addCookie(userIdCK);
			response.addCookie(tenantIdCK);
			response.addCookie(flagCK);

			baseResultModel.setStatus(ResultStatus.SUCCESS.getStatusCode());
			baseResultModel.setMessage(ResultStatus.SUCCESS.getStatusMessage());
		}else {
			baseResultModel.setStatus(ResultStatus.NO_RECORD.getStatusCode());
			baseResultModel.setMessage(ResultStatus.NO_RECORD.getStatusMessage());
		}

		return baseResultModel;
	}

	/**
	 * @description: 找回密码使用
	 *
	 * @return
	 * @author ZhangHao
	 * @date 2018年5月28日 下午6:23:08
	 * @version 1.0.0.1
	 */
	@Override
	public Object ajaxRetrievePwd(SysUserDto dto, HttpServletRequest request) {

		BaseResultModel baseResultModel = new BaseResultModel();

		//获取新密码
		String new_pwd = dto.getPwd();

		// 从cookie中获取当前用户的uid
		Cookie[] cookies = request.getCookies();
		Map cookieSet = CookieUtil.getCookieSet(cookies);
		String flag = (String)cookieSet.get("flag");

		if(StringUtils.isBlank(flag)){
			baseResultModel.setStatus(ResultStatus.HTTP_REQUEST_ERROR.getStatusCode());
			baseResultModel.setMessage("请先验证！");
			return baseResultModel;
		}
		String id = (String)cookieSet.get("uid");
		if(id== null || "".equals(id)){
			baseResultModel.setStatus(ResultStatus.HTTP_REQUEST_ERROR.getStatusCode());
			baseResultModel.setMessage("请刷新页面重试！");
			return baseResultModel;
		}

		Long uid = Long.parseLong(id);
		//密码二次加密
		new_pwd = MD5Util.encodeString(MD5Util.encodeString(new_pwd)+ConfigUtil.MD5_PWD_STR);

		Boolean tag;
		//传参数到service
		try {
			//查询当前要修改密码的用户
			SysUserDto sysUserDto = new SysUserDto();
			sysUserDto.setId(uid);
			SysUser sysUser = accountRpcService.findUserInfoByType(sysUserDto);
			if(sysUser == null) {
				tag = false;
			}else {
				sysUser.setPwd(new_pwd);
				Integer i = accountRpcService.updateSelective(sysUser);
				if(i != 1) {
					tag = false;
				}else {
					tag = true;
				}
			}
		} catch (Exception e) {
			tag = false;
			e.printStackTrace();
		}

		//处理返回结果
		if(!tag){
			baseResultModel.setStatus(ResultStatus.HTTP_REQUEST_ERROR.getStatusCode());
			baseResultModel.setMessage("修改失败，请仔细核对账户和验证码！");
			return baseResultModel;
		}

		baseResultModel.setStatus(ResultStatus.SUCCESS.getStatusCode());
		baseResultModel.setMessage(ResultStatus.SUCCESS.getStatusMessage());

		return baseResultModel;
	}

	/**
	 * @description: 重置密码
	 *
	 * @param dto
	 * @return
	 * @author ZhangHao
	 * @date 2018年5月29日 下午3:35:53
	 * @version 1.0.0.1
	 */
	@Override
	public Object ajaxResetPassword(SysUserDto dto) {

		BaseResultModel baseResultModel = new BaseResultModel();

		SysUser entity = accountRpcService.findUserInfoByType(dto);

		//设置6位随机密码
		String code="";
		String codeMd5 = "";
		Random rand=new Random();//生成随机数
		for(int a=0;a<6;a++){
			code+=rand.nextInt(10);//生成6位随机密码
		}
		//密码二次加密
		codeMd5 = MD5Util.encodeString(MD5Util.encodeString(code) + ConfigUtil.MD5_PWD_STR);
		entity.setPwd(codeMd5);
		entity.setId(dto.getId());
		entity.setUpdateTime(new Date());
		//用户信息入库
		Integer tag = accountRpcService.updateSelective(entity);
		if(tag > 0) {
			baseResultModel.setStatus(ResultStatus.SUCCESS.getStatusCode());
			baseResultModel.setMessage(ResultStatus.SUCCESS.getStatusMessage());
		}else {
			baseResultModel.setStatus(ResultStatus.SUCCESS.getStatusCode());
			baseResultModel.setMessage(ResultStatus.SUCCESS.getStatusMessage());
		}

		return baseResultModel;

	}

	/**
	 * @description: 首次登陆展示用户基础信息
	 *
	 * @return
	 * @author ZhangHao
	 * @date 2018年5月29日 下午9:46:53
	 * @version 1.0.0.1
	 */
	@Override
	public Object ajaxPerInforShow(SysUserDto dto) {

		BaseResultModel baseResultModel = new BaseResultModel();
		//获取用户所绑定的参数
		ShiroSessionData shiroSessionData = ShiroSessionDataUtil.getShiroSessionData();
		dto.setId(shiroSessionData.getUserId());
		SysUser sysUser = accountRpcService.findUserInfoByType(dto);
		if(sysUser != null) {
			baseResultModel.setData(sysUser);
			baseResultModel.setStatus(ResultStatus.SUCCESS.getStatusCode());
			baseResultModel.setMessage(ResultStatus.SUCCESS.getStatusMessage());
		}else {
			baseResultModel.setStatus(ResultStatus.NO_RECORD.getStatusCode());
			baseResultModel.setMessage(ResultStatus.NO_RECORD.getStatusMessage());
		}
		return baseResultModel;
	}


	/**
	 * @description: 首次登陆更新用户基础信息
	 *
	 * @param entity
	 * @return
	 * @author ZhangHao
	 * @date 2018年5月30日 上午9:51:15
	 * @version 1.0.0.1
	 */
	@Override
	public Object ajaxPerInforUpdate(SysUser entity) {
		BaseResultModel baseResultModel = new BaseResultModel();
		//获取用户所绑定的参数
		ShiroSessionData shiroSessionData = ShiroSessionDataUtil.getShiroSessionData();
		entity.setId(shiroSessionData.getUserId());
		entity.setUpdateTime(new Date());
		Integer tag = accountRpcService.updateSelective(entity);
		//如果用户进行了确认更新，那么更新session中的用户名称
		if(tag > 0) {
			shiroSessionData.setUserName(entity.getName());
		}
		Subject subject = SecurityUtils.getSubject();
		Session session = subject.getSession();
		/** 将用户绑定信息对象转换成JSON*/
		String shiroSessionDataStr = JSON.toJSONString(shiroSessionData);

		/** 把用户绑定信息放到全局登录会话中进行维护*/
		session.setAttribute("sessionUserData", shiroSessionDataStr);

		baseResultModel.setStatus(ResultStatus.SUCCESS.getStatusCode());
		baseResultModel.setMessage(ResultStatus.SUCCESS.getStatusMessage());
		return baseResultModel;
	}


	/**
	 * @description: 初始化企业超级管理员账号
	 * 注：
	 * 1、企业需要在company_info按顺序手动构建，并初始化短信充值数量，对应的库由cid进行匹配，如cid = 1 对应 cgroup_00001库
	 * 2、初始化客户用户之前要确保对应的客户库结构一致，冗余数据清空
	 *
	 * 所需参数：
	 * cid - 企业id
	 * account - 客户手机号
	 * name - 客户名称
	 * smsNum - 短信充值数量
	 * pwd - 客户密码
	 *
	 * 请求示例：/account/initCustomerAccount?xxx=xxx&xxx=xxx
	 *
	 * @return
	 * @author ZhangHao
	 * @date 2018年6月2日 上午10:53:21
	 * @version 1.0.0.1
	 */
	@Override
	public Object initCustomerAccount(SysUserDto dto) {
		BaseResultModel baseResultModel = new BaseResultModel();

		//1、根据cid创建一条该企业的部门信息，用于绑定用户的所属部门
		CompanyDepartment companyDepartment = new CompanyDepartment();
		companyDepartment.setCid(dto.getCid());
		companyDepartment.setIsDeleted(0);
		companyDepartment.setName("总部");
		companyDepartment.setParentDeptId(0l);
		companyDepartment.setDescription(dto.getName() + "初始化部门");
		companyDepartment.setCompanyType(1);
		companyDepartment.setLeaderName(dto.getName());
		/** 初始化部门的时候新增一个部门编码*/
		String randomcode = "";
		// 用字符数组的方式随机
		String model = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
		char[] m = model.toCharArray();
		for (int j = 0; j < 6; j++) {
			char c = m[(int) (Math.random() * 36)];
			// 保证六位随机数之间没有重复的
			if (randomcode.contains(String.valueOf(c))) {
				j--;
				continue;
			}
			randomcode = randomcode + c;
		}
		companyDepartment.setCode(randomcode);

		companyDepartment = departmentRpcService.insertSelective(companyDepartment);


		//3、根据cid创建该企业下的4个角色，1个超级管理员角色，3个CRM角色（默认全部的菜单权限、按钮权限），
		//创建后运营可以在角色管理中勾选该客户实际需要的权限，选好后，在数据库中一条delete语句就可以删除不需要给该客户开通的权限
		/** 1、增加酷客超级管理员*/
		SysRole sysRole = new SysRole();
		sysRole.setName("超级管理员");
		sysRole.setCid(dto.getCid());
		sysRole.setIsDeleted(0);
		sysRole.setDescription("客户初始化超级管理员角色");
		sysRole.setProductId(0l);
		sysRole.setExternalAuthority("1,0");
		sysRole = roleRpcService.insertSelective(sysRole);

		/** 2、增加CRM品牌管理员隐藏角色 */
		SysRole sysRole1 = new SysRole();
		sysRole1.setName("CRM品牌管理员");
		sysRole1.setCid(dto.getCid());
		sysRole1.setIsDeleted(0);
		sysRole1.setDescription("客户初始化CRM品牌管理员角色");
		sysRole1.setProductId(1l);
		sysRole1.setProductRoleId(1l);
		sysRole1 = roleRpcService.insertSelective(sysRole1);

		/** 3、增加CRM店长隐藏角色 */
		SysRole sysRole2 = new SysRole();
		sysRole2.setName("CRM店长");
		sysRole2.setCid(dto.getCid());
		sysRole2.setIsDeleted(0);
		sysRole2.setDescription("客户初始化CRM店长角色");
		sysRole2.setProductId(1l);
		sysRole2.setProductRoleId(2l);
		sysRole2 = roleRpcService.insertSelective(sysRole2);

		/** 4、增加CRM店员隐藏角色 */
		SysRole sysRole3 = new SysRole();
		sysRole3.setName("CRM店员");
		sysRole3.setCid(dto.getCid());
		sysRole3.setIsDeleted(0);
		sysRole3.setDescription("客户初始化CRM店员角色");
		sysRole3.setProductId(1l);
		sysRole3.setProductRoleId(3l);
		sysRole3 = roleRpcService.insertSelective(sysRole3);
		//拿到全部的权限
		List<ShiroSysAuthorityBean> authList = accountRpcService.findMenuListByDto(new SysAuthorityDto());
		for(int i=0;i<authList.size();i++) {

			/** 5、增加酷客超级管理员对应的一组权限(默认全部)*/
			//如果不是外部服务菜单
			if(!authList.get(i).getProductId().equals(1l)) {
				SysRoleAuthority sysRoleAuthority = new SysRoleAuthority();
				sysRoleAuthority.setIsDeleted(0);
				sysRoleAuthority.setRoleId(sysRole.getId());
				sysRoleAuthority.setAuthorityId(authList.get(i).getId());
				sysRoleAuthority.setAuthorityName(authList.get(i).getName());
				sysRoleAuthority.setAuthorityNameEn(authList.get(i).getNameEn());
				roleRpcService.insertSysRoleAuthority(sysRoleAuthority);
				/** 6、增加CRM品牌管理员隐藏角色对应的一组权限(默认全部)*/
			}else if(
					"100000147".equals(String.valueOf(authList.get(i).getId())) ||
							"100000148".equals(String.valueOf(authList.get(i).getId())) ||
							"100000149".equals(String.valueOf(authList.get(i).getId())) ||
							"100000150".equals(String.valueOf(authList.get(i).getId())) ||
							"100000151".equals(String.valueOf(authList.get(i).getId())) ||
							"100000152".equals(String.valueOf(authList.get(i).getId())) ||
							"100000153".equals(String.valueOf(authList.get(i).getId())) ||
							"100000154".equals(String.valueOf(authList.get(i).getId())) ||
							"100000155".equals(String.valueOf(authList.get(i).getId())) ||
							"100000156".equals(String.valueOf(authList.get(i).getId())) ||
							"100000157".equals(String.valueOf(authList.get(i).getId())) ||
							"100000158".equals(String.valueOf(authList.get(i).getId()))
					) {
				SysRoleAuthority sysRoleAuthority = new SysRoleAuthority();
				sysRoleAuthority.setIsDeleted(0);
				sysRoleAuthority.setRoleId(sysRole1.getId());
				sysRoleAuthority.setAuthorityId(authList.get(i).getId());
				sysRoleAuthority.setAuthorityName(authList.get(i).getName());
				sysRoleAuthority.setAuthorityNameEn(authList.get(i).getNameEn());
				roleRpcService.insertSysRoleAuthority(sysRoleAuthority);
				/** 7、增加CRM店长隐藏角色对应的一组权限(默认全部)*/
			}else if(
					"100000159".equals(String.valueOf(authList.get(i).getId())) ||
							"100000160".equals(String.valueOf(authList.get(i).getId())) ||
							"100000161".equals(String.valueOf(authList.get(i).getId())) ||
							"100000162".equals(String.valueOf(authList.get(i).getId())) ||
							"100000163".equals(String.valueOf(authList.get(i).getId())) ||
							"100000164".equals(String.valueOf(authList.get(i).getId())) ||
							"100000165".equals(String.valueOf(authList.get(i).getId())) ||
							"100000166".equals(String.valueOf(authList.get(i).getId())) ||
							"100000167".equals(String.valueOf(authList.get(i).getId())) ||
							"100000168".equals(String.valueOf(authList.get(i).getId())) ||
							"100000169".equals(String.valueOf(authList.get(i).getId())) ||
							"100000170".equals(String.valueOf(authList.get(i).getId())) ||
							"100000171".equals(String.valueOf(authList.get(i).getId())) ||
							"100000172".equals(String.valueOf(authList.get(i).getId()))
					) {

				SysRoleAuthority sysRoleAuthority = new SysRoleAuthority();
				sysRoleAuthority.setIsDeleted(0);
				sysRoleAuthority.setRoleId(sysRole2.getId());
				sysRoleAuthority.setAuthorityId(authList.get(i).getId());
				sysRoleAuthority.setAuthorityName(authList.get(i).getName());
				sysRoleAuthority.setAuthorityNameEn(authList.get(i).getNameEn());
				roleRpcService.insertSysRoleAuthority(sysRoleAuthority);
				/** 8、增加CRM店员隐藏角色对应的一组权限(默认全部)*/
			}else if(
					"100000173".equals(String.valueOf(authList.get(i).getId())) ||
							"100000174".equals(String.valueOf(authList.get(i).getId())) ||
							"100000175".equals(String.valueOf(authList.get(i).getId())) ||
							"100000176".equals(String.valueOf(authList.get(i).getId())) ||
							"100000177".equals(String.valueOf(authList.get(i).getId())) ||
							"100000178".equals(String.valueOf(authList.get(i).getId())) ||
							"100000179".equals(String.valueOf(authList.get(i).getId())) ||
							"100000180".equals(String.valueOf(authList.get(i).getId())) ||
							"100000181".equals(String.valueOf(authList.get(i).getId())) ||
							"100000182".equals(String.valueOf(authList.get(i).getId())) ||
							"100000183".equals(String.valueOf(authList.get(i).getId())) ||
							"100000184".equals(String.valueOf(authList.get(i).getId()))
					) {

				SysRoleAuthority sysRoleAuthority = new SysRoleAuthority();
				sysRoleAuthority.setIsDeleted(0);
				sysRoleAuthority.setRoleId(sysRole3.getId());
				sysRoleAuthority.setAuthorityId(authList.get(i).getId());
				sysRoleAuthority.setAuthorityName(authList.get(i).getName());
				sysRoleAuthority.setAuthorityNameEn(authList.get(i).getNameEn());
				roleRpcService.insertSysRoleAuthority(sysRoleAuthority);

			}

		}

		//4、根据cid创建该企业sys_attribute表中的两条数据权限
		SysAttribute sysAttributeStore = new SysAttribute();
		sysAttributeStore.setCid(dto.getCid());
		sysAttributeStore.setIsDeleted(0);
		sysAttributeStore.setAttributeName("store");
		sysAttributeStore.setAttributeSeat("base_store_info");
		sysAttributeStore = roleRpcService.insertSysAttribute(sysAttributeStore);

		SysAttribute sysAttributeDepartment = new SysAttribute();
		sysAttributeDepartment.setCid(dto.getCid());
		sysAttributeDepartment.setIsDeleted(0);
		sysAttributeDepartment.setAttributeName("department");
		sysAttributeDepartment.setAttributeSeat("company_department");
		sysAttributeDepartment = roleRpcService.insertSysAttribute(sysAttributeDepartment);
		//5、为酷客超级管理员角色增加角色与数据权限的对应关系
		//增加角色门店数据前线属性
		SysRoleAttribute sysRoleAttributeStore = new SysRoleAttribute();
		sysRoleAttributeStore.setCid(dto.getCid());
		sysRoleAttributeStore.setIsDeleted(0);
		sysRoleAttributeStore.setRoleId(sysRole.getId());
		sysRoleAttributeStore.setAttributeId(sysAttributeStore.getId());
		roleRpcService.insertSysRoleAttribute(sysRoleAttributeStore);
		//增加角色部门数据权限属性
		SysRoleAttribute sysRoleAttributeDepartment = new SysRoleAttribute();
		sysRoleAttributeDepartment.setCid(dto.getCid());
		sysRoleAttributeDepartment.setIsDeleted(0);
		sysRoleAttributeDepartment.setRoleId(sysRole.getId());
		sysRoleAttributeDepartment.setAttributeId(sysAttributeDepartment.getId());
		roleRpcService.insertSysRoleAttribute(sysRoleAttributeDepartment);

		//6、根据cid、客户名称、地址等调用CRM同步品牌接口，将这个品牌进行同步
		SysUser sysUser = new SysUser();

		//根据cid创建一条该企业的门店信息，用于绑定用户的所属门店(先初始化品牌，再初始化门店)
		BaseStoreInfo baseStoreInfo = new BaseStoreInfo();
		baseStoreInfo.setCid(dto.getCid());
		baseStoreInfo.setIsDeleted(0);
		baseStoreInfo.setName("总部 ");
		baseStoreInfo.setAddress("初始化门店地址");
		baseStoreInfo.setPhone(dto.getAccount());
		baseStoreInfo.setPwd("98abcac65eb64d0cbcf292675e845b59");
		baseStoreInfo.setProvinceId(110000);
		baseStoreInfo.setCityId(110100);
		baseStoreInfo.setAreaId(110101);
		baseStoreInfo.setLng("116.379278");
		baseStoreInfo.setLat("39.921992");
		baseStoreInfo = baseStoreInfoRpcService.insertSelective(baseStoreInfo);

		//7、根据account、pwd、name、departmentId、storeId创建一条用户信息
		sysUser.setPwd("f0d0f52099eae66cf8b13e06061546ea");
		sysUser.setName(dto.getName());
		sysUser.setDepartmentId(companyDepartment.getId());
		sysUser.setIsDeleted(0);
		sysUser.setStatus(0);
		sysUser.setAgreement(0);
		sysUser.setIsFirstLogin(0);
		sysUser.setIsSuper(1);
		sysUser.setUserIdentity(1);
		sysUser = accountRpcService.insertSelective(sysUser);
		//增加用户和门店的对应关系
		BaseStoreToUser baseStoreToUser = new BaseStoreToUser();
		baseStoreToUser.setIsDeleted(0);;
		baseStoreToUser.setCid(dto.getCid());
		baseStoreToUser.setUserId(sysUser.getId());
		baseStoreToUser.setStoreId(baseStoreInfo.getId());
		accountRpcService.insertBaseStoreToUser(baseStoreToUser);
		//增加用户和酷客角色的对应关系
		SysUserRole sysUserRoleCecook = new SysUserRole();
		sysUserRoleCecook.setIsDeleted(0);
		sysUserRoleCecook.setRoleId(sysRole.getId());
		sysUserRoleCecook.setUserId(sysUser.getId());
		accountRpcService.insertUserRoles(sysUserRoleCecook);
		//增加用户和CRM角色的对应关系
		SysUserRole sysUserRoleCrm = new SysUserRole();
		sysUserRoleCrm.setIsDeleted(0);
		sysUserRoleCrm.setRoleId(sysRole1.getId());
		sysUserRoleCrm.setUserId(sysUser.getId());
		accountRpcService.insertUserRoles(sysUserRoleCrm);

		//9、企业客户创建完毕
		baseResultModel.setStatus(ResultStatus.SUCCESS.getStatusCode());
		baseResultModel.setMessage(ResultStatus.SUCCESS.getStatusMessage());
		return baseResultModel;
	}

	/**
	 * @description: 手机页面重置密码
	 *
	 * @param dto
	 * @author ZhangHao
	 * @date 2018年6月4日 下午8:06:40
	 * @version 1.0.0.1
	 */
	public BaseResultModel ajaxClientResetPassword(SysUserDto dto) {
		BaseResultModel r = new BaseResultModel();
		SysUser entity = accountRpcService.findUserInfoByType(dto);
		if(entity == null) {
			r.setStatus(ResultStatus.NO_RECORD.getStatusCode());
			r.setMessage(ResultStatus.NO_RECORD.getStatusMessage());
			return r;
		}

		//密码二次加密
		String code = MD5Util.encodeString(MD5Util.encodeString(dto.getPwd()) + ConfigUtil.MD5_PWD_STR);
		SysUser e = new SysUser();
		e.setPwd(code);
		e.setId(entity.getId());
		e.setUpdateTime(new Date());
		//用户信息入库
		Integer flag = accountRpcService.updateSelective(e);
		if(flag != 1) {
			r.setStatus(ResultStatus.UPDATE_ERROR.getStatusCode());
			r.setMessage("密码更新失败");
			return r;
		}

		r.setStatus(ResultStatus.SUCCESS.getStatusCode());
		r.setMessage(ResultStatus.SUCCESS.getStatusMessage());
		return r;
	}

	/**
	 * @description: 弹框列表|分页,表格形式，上面的方法是选项卡形式
	 *
	 * @param
	 * @author ZhangHao
	 * @date 2018/8/2 13:30
	 * @version 1.0.0.1
	 * @return java.lang.Object
	 */

	@Override
	public Object ajaxAccountTableUserList(String param) {
		JSONObject paramObj = JSONObject.parseObject(param);
		int startIndex = paramObj.getInteger("start");
		int pageSize = paramObj.getInteger("length");
		int draw = paramObj.getInteger("draw");
		JSONObject obj = paramObj.getJSONObject("extra_search");
		String keyWord = "";
		if(obj != null) {
			keyWord = obj.getString("keyWord");
		}

		//获取用户所绑定的参数
		ShiroSessionData shiroSessionData = ShiroSessionDataUtil.getShiroSessionData();
		SysUserDto dto = new SysUserDto();
		dto.setStartIndex(startIndex);
		dto.setPageSize(pageSize);
		dto.setCid(shiroSessionData.getCid());
		dto.setKeyWord(keyWord);
		PageInfo<SysUser> dialogPageList = accountRpcService.ajaxPageUserListByType(dto);
		if(dialogPageList != null) {
			for (int i = 0; i < dialogPageList.getList().size(); i++) {
				BaseStoreToUserDto bstu = new BaseStoreToUserDto();
				bstu.setCid(shiroSessionData.getCid());
				bstu.setUserId(dialogPageList.getList().get(i).getId());
				List<BaseStoreToUser> bstuList = accountRpcService.selectUserStores(bstu);
				BaseStoreInfoDto bsid = new BaseStoreInfoDto();
				bsid.setCid(shiroSessionData.getCid());
				List<BaseStoreInfo> bsiList = baseStoreInfoRpcService.findListByDto(bsid);

				if (bstuList == null || bstuList.size() == 0 || bsiList == null || bsiList.size() == 0) {
					continue;
				}
				String str = "";
				for (BaseStoreToUser bs : bstuList) {
					for (BaseStoreInfo bi : bsiList) {
						if (bs.getStoreId().longValue() == bi.getId().longValue()) {
							str += bi.getName() + ",";
						}
					}
				}

				if (StringUtils.isNotBlank(str)) {
					dialogPageList.getList().get(i).setStores(str.substring(0, str.length() - 1));
				}
			}
		}

		Map<String, Object> resultMap = new HashMap<String, Object>();
		if(dialogPageList != null) {
			resultMap.put("recordsTotal", dialogPageList.getTotal());
			resultMap.put("recordsFiltered", dialogPageList.getTotal());
			resultMap.put("data", dialogPageList.getList());
			resultMap.put("draw", draw);
		}else{
			resultMap.put("recordsTotal", 0);
			resultMap.put("recordsFiltered", 0);
			resultMap.put("data", Collections.emptyList());
			resultMap.put("draw", draw);
		}
		return resultMap;
	}

	/**
	 * @description: 账号开通服务使用，增加可以选择权限范围，创建企业的功能
	 *
	 * @param
	 * @author ZhangHao
	 * @date 2018/10/23 14:36
	 * @version 1.0.0.1
	 * @return java.lang.Object
	 */
	@Override
	public Object initCustomerAccountService(SysUserDto dto) {
		BaseResultModel baseResultModel = new BaseResultModel();

		CompanyInfo companyInfo = new CompanyInfo();
		companyInfo.setName(dto.getCompanyName());
		companyInfo.setAddress(dto.getCompanyAddress());
		if(dto.getSmsNum() == null){
			companyInfo.setSmsNum(0);
		}else{
			companyInfo.setSmsNum(dto.getSmsNum());
		}
		//创建一条公司信息并返回cid
		companyInfo = companyInfoRpcService.insertSelective(companyInfo);
		companyInfo.setCreateSequenceCode(companyInfo.getId().intValue());
		companyInfoRpcService.updateSelective(companyInfo);

		//1、根据cid创建一条该企业的部门信息，用于绑定用户的所属部门
		CompanyDepartment companyDepartment = new CompanyDepartment();
		companyDepartment.setCid(companyInfo.getId());
		companyDepartment.setIsDeleted(0);
		companyDepartment.setName("总部");
		companyDepartment.setParentDeptId(0l);
		companyDepartment.setDescription(dto.getName() + "初始化部门");
		companyDepartment.setCompanyType(1);
		companyDepartment.setLeaderName(dto.getName());
		/** 初始化部门的时候新增一个部门编码*/
		String randomcode = "";
		// 用字符数组的方式随机
		String model = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
		char[] m = model.toCharArray();
		for (int j = 0; j < 6; j++) {
			char c = m[(int) (Math.random() * 36)];
			// 保证六位随机数之间没有重复的
			if (randomcode.contains(String.valueOf(c))) {
				j--;
				continue;
			}
			randomcode = randomcode + c;
		}
		companyDepartment.setCode(randomcode);

		companyDepartment = departmentRpcService.insertSelective(companyDepartment);


		//3、根据cid创建该企业下的4个角色，1个超级管理员角色，3个CRM角色（默认全部的菜单权限、按钮权限），
		//创建后运营可以在角色管理中勾选该客户实际需要的权限，选好后，在数据库中一条delete语句就可以删除不需要给该客户开通的权限
		/** 1、增加酷客超级管理员*/
		SysRole sysRole = new SysRole();
		sysRole.setName("超级管理员");
		sysRole.setCid(companyInfo.getId());
		sysRole.setIsDeleted(0);
		sysRole.setDescription("客户初始化超级管理员角色");
		sysRole.setProductId(0l);
		sysRole.setExternalAuthority("1,0");
		sysRole = roleRpcService.insertSelective(sysRole);

		/** 2、增加CRM品牌管理员隐藏角色 */
		SysRole sysRole1 = new SysRole();
		sysRole1.setName("CRM品牌管理员");
		sysRole1.setCid(companyInfo.getId());
		sysRole1.setIsDeleted(0);
		sysRole1.setDescription("客户初始化CRM品牌管理员角色");
		sysRole1.setProductId(1l);
		sysRole1.setProductRoleId(1l);
		sysRole1 = roleRpcService.insertSelective(sysRole1);

		/** 3、增加CRM店长隐藏角色 */
		SysRole sysRole2 = new SysRole();
		sysRole2.setName("CRM店长");
		sysRole2.setCid(companyInfo.getId());
		sysRole2.setIsDeleted(0);
		sysRole2.setDescription("客户初始化CRM店长角色");
		sysRole2.setProductId(1l);
		sysRole2.setProductRoleId(2l);
		sysRole2 = roleRpcService.insertSelective(sysRole2);

		/** 4、增加CRM店员隐藏角色 */
		SysRole sysRole3 = new SysRole();
		sysRole3.setName("CRM店员");
		sysRole3.setCid(companyInfo.getId());
		sysRole3.setIsDeleted(0);
		sysRole3.setDescription("客户初始化CRM店员角色");
		sysRole3.setProductId(1l);
		sysRole3.setProductRoleId(3l);
		sysRole3 = roleRpcService.insertSelective(sysRole3);
		//拿到全部的权限
		List<ShiroSysAuthorityBean> authList = accountRpcService.findMenuListByDto(new SysAuthorityDto());
		String[] authoritIdListArr = dto.getAccessToken().split(",");

		for(int i=0;i<authList.size();i++) {

			/** 5、增加酷客超级管理员对应的一组权限(默认全部)*/
			//如果不是外部服务菜单
			if(!authList.get(i).getProductId().equals(1l)) {
				//如果菜单在这个范围，才加入入库结果集
				for(int k=0;k<authoritIdListArr.length;k++){
					String[] idNameArr = authoritIdListArr[k].split("&");
					if(authList.get(i).getId().equals(Long.valueOf(idNameArr[0]))){
						SysRoleAuthority sysRoleAuthority = new SysRoleAuthority();
						sysRoleAuthority.setIsDeleted(0);
						sysRoleAuthority.setRoleId(sysRole.getId());
						sysRoleAuthority.setAuthorityId(authList.get(i).getId());
						sysRoleAuthority.setAuthorityName(authList.get(i).getName());
						sysRoleAuthority.setAuthorityNameEn(authList.get(i).getNameEn());
						roleRpcService.insertSysRoleAuthority(sysRoleAuthority);
					}
				}
				/** 6、增加CRM品牌管理员隐藏角色对应的一组权限(默认全部)*/
			}else if(
					"100000147".equals(String.valueOf(authList.get(i).getId())) ||
							"100000148".equals(String.valueOf(authList.get(i).getId())) ||
							"100000149".equals(String.valueOf(authList.get(i).getId())) ||
							"100000150".equals(String.valueOf(authList.get(i).getId())) ||
							"100000151".equals(String.valueOf(authList.get(i).getId())) ||
							"100000152".equals(String.valueOf(authList.get(i).getId())) ||
							"100000153".equals(String.valueOf(authList.get(i).getId())) ||
							"100000154".equals(String.valueOf(authList.get(i).getId())) ||
							"100000155".equals(String.valueOf(authList.get(i).getId())) ||
							"100000156".equals(String.valueOf(authList.get(i).getId())) ||
							"100000157".equals(String.valueOf(authList.get(i).getId())) ||
							"100000158".equals(String.valueOf(authList.get(i).getId()))
					) {
				SysRoleAuthority sysRoleAuthority = new SysRoleAuthority();
				sysRoleAuthority.setIsDeleted(0);
				sysRoleAuthority.setRoleId(sysRole1.getId());
				sysRoleAuthority.setAuthorityId(authList.get(i).getId());
				sysRoleAuthority.setAuthorityName(authList.get(i).getName());
				sysRoleAuthority.setAuthorityNameEn(authList.get(i).getNameEn());
				roleRpcService.insertSysRoleAuthority(sysRoleAuthority);
				/** 7、增加CRM店长隐藏角色对应的一组权限(默认全部)*/
			}else if(
					"100000159".equals(String.valueOf(authList.get(i).getId())) ||
							"100000160".equals(String.valueOf(authList.get(i).getId())) ||
							"100000161".equals(String.valueOf(authList.get(i).getId())) ||
							"100000162".equals(String.valueOf(authList.get(i).getId())) ||
							"100000163".equals(String.valueOf(authList.get(i).getId())) ||
							"100000164".equals(String.valueOf(authList.get(i).getId())) ||
							"100000165".equals(String.valueOf(authList.get(i).getId())) ||
							"100000166".equals(String.valueOf(authList.get(i).getId())) ||
							"100000167".equals(String.valueOf(authList.get(i).getId())) ||
							"100000168".equals(String.valueOf(authList.get(i).getId())) ||
							"100000169".equals(String.valueOf(authList.get(i).getId())) ||
							"100000170".equals(String.valueOf(authList.get(i).getId())) ||
							"100000171".equals(String.valueOf(authList.get(i).getId())) ||
							"100000172".equals(String.valueOf(authList.get(i).getId()))
					) {

				SysRoleAuthority sysRoleAuthority = new SysRoleAuthority();
				sysRoleAuthority.setIsDeleted(0);
				sysRoleAuthority.setRoleId(sysRole2.getId());
				sysRoleAuthority.setAuthorityId(authList.get(i).getId());
				sysRoleAuthority.setAuthorityName(authList.get(i).getName());
				sysRoleAuthority.setAuthorityNameEn(authList.get(i).getNameEn());
				roleRpcService.insertSysRoleAuthority(sysRoleAuthority);
				/** 8、增加CRM店员隐藏角色对应的一组权限(默认全部)*/
			}else if(
					"100000173".equals(String.valueOf(authList.get(i).getId())) ||
							"100000174".equals(String.valueOf(authList.get(i).getId())) ||
							"100000175".equals(String.valueOf(authList.get(i).getId())) ||
							"100000176".equals(String.valueOf(authList.get(i).getId())) ||
							"100000177".equals(String.valueOf(authList.get(i).getId())) ||
							"100000178".equals(String.valueOf(authList.get(i).getId())) ||
							"100000179".equals(String.valueOf(authList.get(i).getId())) ||
							"100000180".equals(String.valueOf(authList.get(i).getId())) ||
							"100000181".equals(String.valueOf(authList.get(i).getId())) ||
							"100000182".equals(String.valueOf(authList.get(i).getId())) ||
							"100000183".equals(String.valueOf(authList.get(i).getId())) ||
							"100000184".equals(String.valueOf(authList.get(i).getId()))
					) {

				SysRoleAuthority sysRoleAuthority = new SysRoleAuthority();
				sysRoleAuthority.setIsDeleted(0);
				sysRoleAuthority.setRoleId(sysRole3.getId());
				sysRoleAuthority.setAuthorityId(authList.get(i).getId());
				sysRoleAuthority.setAuthorityName(authList.get(i).getName());
				sysRoleAuthority.setAuthorityNameEn(authList.get(i).getNameEn());
				roleRpcService.insertSysRoleAuthority(sysRoleAuthority);

			}

		}

		//4、根据cid创建该企业sys_attribute表中的两条数据权限
		SysAttribute sysAttributeStore = new SysAttribute();
		sysAttributeStore.setCid(companyInfo.getId());
		sysAttributeStore.setIsDeleted(0);
		sysAttributeStore.setAttributeName("store");
		sysAttributeStore.setAttributeSeat("base_store_info");
		sysAttributeStore = roleRpcService.insertSysAttribute(sysAttributeStore);

		SysAttribute sysAttributeDepartment = new SysAttribute();
		sysAttributeDepartment.setCid(companyInfo.getId());
		sysAttributeDepartment.setIsDeleted(0);
		sysAttributeDepartment.setAttributeName("department");
		sysAttributeDepartment.setAttributeSeat("company_department");
		sysAttributeDepartment = roleRpcService.insertSysAttribute(sysAttributeDepartment);
		//5、为酷客超级管理员角色增加角色与数据权限的对应关系
		//增加角色门店数据前线属性
		SysRoleAttribute sysRoleAttributeStore = new SysRoleAttribute();
		sysRoleAttributeStore.setCid(companyInfo.getId());
		sysRoleAttributeStore.setIsDeleted(0);
		sysRoleAttributeStore.setRoleId(sysRole.getId());
		sysRoleAttributeStore.setAttributeId(sysAttributeStore.getId());
		roleRpcService.insertSysRoleAttribute(sysRoleAttributeStore);
		//增加角色部门数据权限属性
		SysRoleAttribute sysRoleAttributeDepartment = new SysRoleAttribute();
		sysRoleAttributeDepartment.setCid(companyInfo.getId());
		sysRoleAttributeDepartment.setIsDeleted(0);
		sysRoleAttributeDepartment.setRoleId(sysRole.getId());
		sysRoleAttributeDepartment.setAttributeId(sysAttributeDepartment.getId());
		roleRpcService.insertSysRoleAttribute(sysRoleAttributeDepartment);

		//6、根据cid、客户名称、地址等调用CRM同步品牌接口，将这个品牌进行同步
		SysUser sysUser = new SysUser();

		//根据cid创建一条该企业的门店信息，用于绑定用户的所属门店(先初始化品牌，再初始化门店)
		BaseStoreInfo baseStoreInfo = new BaseStoreInfo();
		baseStoreInfo.setCid(companyInfo.getId());
		baseStoreInfo.setIsDeleted(0);
		baseStoreInfo.setName("总部 ");
		baseStoreInfo.setAddress("初始化门店地址");
		baseStoreInfo.setPhone(dto.getAccount());
		baseStoreInfo.setPwd("98abcac65eb64d0cbcf292675e845b59");
		baseStoreInfo.setProvinceId(110000);
		baseStoreInfo.setCityId(110100);
		baseStoreInfo.setAreaId(110101);
		baseStoreInfo.setLng("116.379278");
		baseStoreInfo.setLat("39.921992");
		baseStoreInfo = baseStoreInfoRpcService.insertSelective(baseStoreInfo);

		//7、根据account、pwd、name、departmentId、storeId创建一条用户信息
		sysUser.setPwd("f0d0f52099eae66cf8b13e06061546ea");
		sysUser.setName(dto.getName());
		sysUser.setDepartmentId(companyDepartment.getId());
		sysUser.setIsDeleted(0);
		sysUser.setStatus(0);
		sysUser.setAgreement(0);
		sysUser.setIsFirstLogin(0);
		sysUser.setIsSuper(1);
		sysUser.setUserIdentity(1);
		sysUser = accountRpcService.insertSelective(sysUser);
		//增加用户和门店的对应关系
		BaseStoreToUser baseStoreToUser = new BaseStoreToUser();
		baseStoreToUser.setIsDeleted(0);;
		baseStoreToUser.setCid(companyInfo.getId());
		baseStoreToUser.setUserId(sysUser.getId());
		baseStoreToUser.setStoreId(baseStoreInfo.getId());
		accountRpcService.insertBaseStoreToUser(baseStoreToUser);
		//增加用户和酷客角色的对应关系
		SysUserRole sysUserRoleCecook = new SysUserRole();
		sysUserRoleCecook.setIsDeleted(0);
		sysUserRoleCecook.setRoleId(sysRole.getId());
		sysUserRoleCecook.setUserId(sysUser.getId());
		accountRpcService.insertUserRoles(sysUserRoleCecook);
		//增加用户和CRM角色的对应关系
		SysUserRole sysUserRoleCrm = new SysUserRole();
		sysUserRoleCrm.setIsDeleted(0);
		sysUserRoleCrm.setRoleId(sysRole1.getId());
		sysUserRoleCrm.setUserId(sysUser.getId());
		accountRpcService.insertUserRoles(sysUserRoleCrm);
		//9、企业客户创建完毕
		baseResultModel.setStatus(ResultStatus.SUCCESS.getStatusCode());
		baseResultModel.setMessage(ResultStatus.SUCCESS.getStatusMessage());
		return baseResultModel;
	}

	/**
	 * @description: 根据手机号展示或隐藏客户CRM权限
	 *
	 * @param
	 * @author ZhangHao
	 * @date 2018/10/23 17:25
	 * @version 1.0.0.1
	 * @return java.lang.Object
	 */
	@Override
	public Object showAndHideCrmMenu(SysUserDto dto) {
		BaseResultModel baseResultModel = new BaseResultModel();

		try {
			//首先根据手机号查询用户基础信息
			SysUserDto sd = new SysUserDto();
			sd.setAccount(dto.getAccount());
			SysUser user = accountRpcService.findUserInfoByType(dto);
			Integer idDeleted = dto.getIsDeleted();
			Long cid = user.getCid();
//			SysUserRoleDto surd = new SysUserRoleDto();
//			surd.setUserId(user.getId());
//			List<SysUserRole> list = accountRpcService.selectSysUserRoles(surd);
			SysRoleDto srd = new SysRoleDto();
			srd.setCid(cid);
			srd.setIsDeleted(2);
			List<SysRole> roleList =roleRpcService.findListByDto(srd);
//			for (SysUserRole sr:list) {
//				SysRoleDto srd = new SysRoleDto();
//				srd.setCid(user.getCid());
//				srd.setId(sr.getId());
//				SysRole sysRole = roleRpcService.selectSysRoleByType(srd);
//				roleList.add(sysRole);
//			}

			for(SysRole r:roleList){
				//开通CRM
				if(idDeleted == 0) {
					if(r.getProductRoleId() != null){
						SysRole sr = new SysRole();
						sr.setIsDeleted(0);
						sr.setCid(cid);
						sr.setId(r.getId());
						sr.setUpdateTime(new Date());
						roleRpcService.updateSelective(sr);
					}
				}else{
					if(r.getProductRoleId() != null){
						SysRole sr = new SysRole();
						sr.setIsDeleted(1);
						sr.setCid(cid);
						sr.setId(r.getId());
						sr.setUpdateTime(new Date());
						roleRpcService.updateSelective(sr);
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		baseResultModel.setStatus(ResultStatus.SUCCESS.getStatusCode());
		baseResultModel.setMessage(ResultStatus.SUCCESS.getStatusMessage());
		return baseResultModel;
	}

	/**
	 * @description: 查询账号创建手机号是否重复
	 *
	 * @param
	 * @author ZhangHao
	 * @date 2018/10/24 14:32
	 * @version 1.0.0.1
	 * @return java.lang.Object
	 */
	@Override
	public Object getAccountIfRepeat(SysUserDto dto) {
		BaseResultModel baseResultModel = new BaseResultModel();


		SysUserDto sd = new SysUserDto();
		sd.setAccount(dto.getAccount());
		SysUser user = accountRpcService.findUserInfoByType(dto);

		if(user != null){
			baseResultModel.setStatus(ResultStatus.NEED_DATA_ERROR.getStatusCode());
			baseResultModel.setMessage(ResultStatus.NEED_DATA_ERROR.getStatusMessage());
		}else{
			baseResultModel.setStatus(ResultStatus.SUCCESS.getStatusCode());
			baseResultModel.setMessage(ResultStatus.SUCCESS.getStatusMessage());
		}

		return baseResultModel;
	}

}


























